/*   
 * IVRWebServiceFaced.java Created on 2006-12-29   
 * Copyright(c) 2006-2008 by vnv   
 * ALL Rights Reserved.   
 */
package cn.agiledata.bank.webService.faced;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.dao.ActionTypeTemplate;
import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.exception.BusinessException;
import cn.agiledata.bank.common.service.ICommonService;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.linkbank.logic.payment.UnsPayRequest;
import cn.agiledata.bank.linkbank.logic.payment.UnsPayResponse;
import cn.agiledata.bank.linkbank.service.WebPayService;
import cn.agiledata.bank.myspace.service.IMySpaceService;
import cn.agiledata.bank.webService.WebServiceUtil;
import cn.agiledata.bank.webService.constant.IVRConstant;
import cn.agiledata.bank.webService.formbean.AccountListFormbean;
import cn.agiledata.bank.webService.formbean.BankFormbean;
import cn.agiledata.bank.webService.formbean.IVRRequest;
import cn.agiledata.bank.webService.formbean.IVRResponse;

import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;
/**
 * TODO to change this type comment
 * 
 * @time: 9:32:06
 * @author ddd
 */
public class IVRWebServiceFaced implements IIVRWebServiceFaced {

	private static Log logger = LogFactory.getLog(IVRWebServiceFaced.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#loginUser(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse loginUser(IVRRequest request) {

		String userid;
		String logonpasswd;
		userid = request.getUserId();
		logonpasswd = request.getLoginpasswd();
		IVRResponse response = new IVRResponse();
		logger.debug(request.toString());
		logger.debug("IVRWebServiceFaced loginUser userid ---> " + userid
				+ " logonpasswd --> " + logonpasswd);
		Map map = new HashMap();
		map.put("username",userid);
		map.put("password",logonpasswd);
		try {
			this.getCommonService().userLogon(Constant.CALL_CENTER, map);
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (Exception ipex) {
			// ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#orderFormPayment(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse orderFormPayment(IVRRequest request) {

		BankInfo bankInfo = new BankInfo();
		IVRResponse response = new IVRResponse();
		UnsPayRequest unspayRequest = new UnsPayRequest();
		
		//bankInfo.setId(new Long(request.getBanktype()));
		unspayRequest.setOrderId(request.getOrderid());
		//unspayRequest.setBank(bankInfo);
		unspayRequest.setAmount(new BigDecimal(request.getMoney().doubleValue()));
		unspayRequest.setCurrencyType(request.getPaymenttype());
		unspayRequest.setTime(new Date());
		long actionTypeid;
		actionTypeid = Long.valueOf(Constant.ACTION_TYPE_C).longValue();
		
		try {

			unspayRequest.setActionType(getActionTemplate().getActionTypeById(
					actionTypeid));
			unspayRequest.setCreditAccount(this.getAccountTemplate()
					.getAccountByAccountId(request.getUserId()));

			UnsPayResponse payResponse=this.getWebPayService().getUnsPayResponse(unspayRequest, true);
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		} catch (Exception ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#accountTopup(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse accountTopup(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 提现
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#accountDistillCash(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse accountDistillCash(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		Map parmMap = new HashMap();
		parmMap = new HashMap();
		String accountid = request.getUserId(); //帐户编号
		String loginPasswd = request.getLoginpasswd();	//登陆密码 
		String bankcard = request.getBankcard();	//银行卡id
		String money = request.getMoney().toString();	//金额
		String payPaymentpasswd = request.getPaymentpasswd();	//支付密码
		
		String step = request.getStep();	//步骤

		parmMap.put("accountId", accountid); // 帐户编号
		parmMap.put("loginpasswd", loginPasswd); // 登陆密码
		parmMap.put("bankNo", bankcard); // 银行卡id
		parmMap.put("money", money); // 金额
		parmMap.put("payPassword", payPaymentpasswd); // 支付密码
		parmMap.put("actionType", Constant.ACTION_TYPE_D);	//提现
		

		try {
			Transaction trans = this.getCommonService().transfer(Constant.CALL_CENTER,
					step, parmMap);
			response.setBankAccount(this.getCommonService().getAccountBankById(trans.getAccountBankSeq()).getBankAlias());//收款帐户
			response.setBankcard(trans.getCardNo());	//卡号
			response.setMoney(trans.getAmount());//金额
			response.setDebitFee(trans.getDebitFee().toString());	//收款方手续费
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);//
			response.setScoresAmount(trans.getScoresAmount());//本次积分
			response.setTotalScores(trans.getTotalScores());//累计积分
			
		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		} catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 1.3	网上付款接口
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#accountTransfer(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse accountTransfer(IVRRequest request) {

		Map vnvPameMap = new HashMap();
		IVRResponse response = new IVRResponse();
		long accountId;
		String subaccountid;
		String receiveaccountid = request.getCredited();
		String moneyvnv = request.getMoney().toString();
		String paymentPasswd = request.getPaymentpasswd();
		String accountidvnv = request.getUserId();
		vnvPameMap.put("accountId", accountidvnv); //用户编号
		vnvPameMap.put("receiveAccountName", receiveaccountid); // 收款方客户编号
		vnvPameMap.put("money", moneyvnv); //金额
		vnvPameMap.put("payPassword", paymentPasswd); // 支付密码
		String step = request.getStep();	//步骤
		
		
		try {
			
			accountId=Long.parseLong(request.getUserId());
			subaccountid = this.getIVRSubAccount(accountId).getId().toString();	//ivr子帐户
			vnvPameMap.put("subAccountId",subaccountid);
			Transaction trans = this.getCommonService().vnvpayment(Constant.CALL_CENTER,
					step, vnvPameMap);
			response.setReceiveAccountName(trans.getCredit().getAccount().getAccountId());	//收款人帐户编号
			response.setMoney(trans.getAmount());//金额
			response.setDebitFee(trans.getDebitFee().toString());	//收款方手续费		
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}catch(Exception ex){
			ex.printStackTrace();
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#accountPayment(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse accountPayment(IVRRequest request) {

		IVRResponse response = new IVRResponse();
		Map parmMap = new HashMap();
		long accountId;
		parmMap = new HashMap();
		String accountid = request.getUserId();
		String subaccountid = null;
		String payPaymentpasswd = request.getPaymentpasswd();
		String bankName = request.getBanktype();
		String bankNo = request.getBankcard();
		String money = request.getMoney().toString();

		parmMap.put("accountId", accountid); // wap:账户id;call
		// center:账户accountId
		//parmMap.put("subAccountId", "1143"); // wap:子账户id
		parmMap.put("payPassword", payPaymentpasswd); // wap:支付密码;call
		// center:IVR支付密码
		parmMap.put("bankName", bankName); // 所属银行
		parmMap.put("bankNo", bankNo); // 卡号
		parmMap.put("money", money); // 金额
		parmMap.put("name", accountid); // 姓名
		parmMap.put("actionType", Constant.ACTION_TYPE_G);//付款给对方银行

		try {
			
			
			accountId=Long.parseLong(request.getUserId());
			subaccountid = this.getIVRSubAccount(accountId).getId().toString();
			parmMap.put("subAccountId", subaccountid); // wap:子账户id
			
			this.getCommonService().transfer(Constant.CALL_CENTER,
					Constant.STEP_CONFIRM, parmMap);
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		} catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error(ex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#bankCardChange(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse bankCardChange(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		return response;
	}

//	/*
//	 * (non-Javadoc)
//	 * 
//	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#searchAccount(cn.agiledata.bank.webService.formbean.IVRRequest)
//	 */
	public IVRResponse searchAccount(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#getSubAccountList(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse getSubAccountList(IVRRequest request) {

		String userid;
		String logonpasswd;
		userid = request.getUserId();
		logonpasswd = request.getLoginpasswd();
		IVRResponse response = new IVRResponse();
		Map map = new HashMap();
		map.put("username",userid);
		map.put("password",logonpasswd);
		
		Account account = this.getAccountByid(Long.parseLong(userid));
		//List list = account.getSubAccounts();
		//return subaccount;
		try {

			AccountSummary accountSummary = getCommonService().userLogon(
					Constant.CALL_CENTER, map);
			
			List list = accountSummary.getSubAccountList();
			response.setArrayLength(new Integer(list.size()));
			response.setSubAccountArray(WebServiceUtil
					.convertList2SubAccounts(list));
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);

		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#searchSubaccountBlance(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse searchSubaccountBlance(IVRRequest request) {

		String userid;
		String logonpasswd;
		
		userid = request.getUserId();
		logonpasswd = request.getLoginpasswd();
		IVRResponse response = new IVRResponse();

		
		
		

		Map map = new HashMap();
		map.put("username",userid);
		map.put("password",logonpasswd);

		try {

			
			
			
			SubAccount subAccount = this.getIVRSubAccount(Long.parseLong(userid));//得到ivr子帐户
			response.setSubAccountName(subAccount.getAccountName());
			response.setMoney(subAccount.getAccountBalance().getBalance());
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
			//List list = accountSummary.getSubAccountList();
			//for (int i = 0; i < list.size(); i++) {
			//	subaccount = (SubAccount) list.get(i);
			//	if (subAccountid.equals(subaccount.getId().toString())) {
			//		response.setMoney(subaccount.getAccountBalance()
			//				.getBalance());
			//		response.setResult(IVRConstant.COMMON_RESULT_TRUE);
			//		continue;
			//	}
			//}
			

		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#searchIntegral(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse searchIntegral(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		String userid = request.getUserId();
		try {
			
			Account account=getCommonService().getAccountByAccountID(userid);
			if(account==null)
				account=this.getAccountByid(Long.parseLong(userid));
			if(account==null){
				response.setResult(IVRConstant.COMMON_RESULT_FAIL);
			}else{
				response.setResult(IVRConstant.COMMON_RESULT_TRUE);
				response.setIntegral(account.getTotalScores());
			}
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipbex) {
			ipbex.printStackTrace();
			logger.error(ipbex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#searchTransactionHistory(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse searchTransactionHistory(IVRRequest request) {

		List list = new ArrayList();
		Map paramMap = new HashMap();
		String userid = request.getUserId();

		String logonpasswd = request.getLoginpasswd();
		IVRResponse response = new IVRResponse();

		String accountid = String.valueOf(new Long(this.getAccountSummary(userid,logonpasswd).getAccountId()));
		
		//paramMap.put("beginDate", beginDate);
		//paramMap.put("endDate", endDate);
		//paramMap.put("subAccountID", subAccountid);
		paramMap.put("accountID",accountid);
		try {
			list = getMySpaceService().getAccountHisListPageBean(paramMap, 1, 5).getData();
			if(list == null) {
				throw BusinessException.NO_RESULT;
			}
			if(list.size()==0) {
				throw BusinessException.NO_RESULT;
			}
			
			response.setTransactionArray(WebServiceUtil
					.convertList2Transactions(list));
			response.setArrayLength(new Integer(list.size()));
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		} catch (IPBException ipbex) {
			ipbex.printStackTrace();
			logger.error(ipbex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#searchWaitTransaction(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse searchWaitTransaction(IVRRequest request) {

		String accountId = request.getAccounted();
		IVRResponse response = new IVRResponse();
		
			
		long accountidlong = this.getAccountSummary(request.getUserId(),request.getLoginpasswd()).getAccountId();	
		
		//TransactionListPage pl = new TransactionListPage(3, accountidlong);
		
		try{
			PageBean pb = this.getCommonService().getTransactionList(1,5,accountidlong);
			List list = pb.getData();
			if(list == null) {
				throw BusinessException.NO_RESULT;
			}
			if(list.size()==0) {
				throw BusinessException.NO_RESULT;
			}			
			logger.debug("searchWaitTransaction request --> "+ response.toString());
			response.setTransactionArray(WebServiceUtil.convertList2WaitTransactions(list));
			response.setArrayLength(new Integer(list.size()));
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		} catch (IPBException ipbex) {
			ipbex.printStackTrace();
			logger.error(ipbex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}
		
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#modifyPhoneLoginPasswd(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse modifyPhoneLoginPasswd(IVRRequest request) {
		
		
		IVRResponse response = new IVRResponse();
		String userid = request.getUserId();
		String newpasswd = request.getNewpasswd();
		long accoungidlong = getAccountSummary(userid,request.getLoginpasswd()).getAccountId();
		String accountid = String.valueOf(new Long(accoungidlong));
		try {
			getCommonService().loginPasswordMofify(Constant.CALL_CENTER,
					userid, newpasswd);
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipbex) {
			ipbex.printStackTrace();
			logger.error(ipbex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		} 

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#modifyPhoneLoginPaymentPasswd(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse modifyPhoneLoginPaymentPasswd(IVRRequest request) {

		IVRResponse response = new IVRResponse();
		String userid = request.getUserId();
		String originalpasswd = request.getOriginalpasswd();
		String newpasswd = request.getNewpasswd();
		try {
			
			
			getCommonService().payPasswordMofify(Constant.CALL_CENTER, userid,originalpasswd,newpasswd);
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipbex) {
			ipbex.printStackTrace();
			logger.error(ipbex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#setPhoneLoginPaymentPasswd(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse setPhoneLoginPaymentPasswd(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		String userid = request.getUserId();
		String paypasswd = request.getPaymentpasswd();
		try {
			getCommonService().payPasswordMofify(Constant.CALL_CENTER, userid,"",
					paypasswd);
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);			
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResultDescription(be.getReturnCode());
		    response.setResult(be.getResultCode());
		}catch (IPBException ipbex) {
			ipbex.printStackTrace();
			logger.error(ipbex);
			response.setResultDescription(ipbex.getDetails());
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#modifyTransactionBiglimit(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse modifyTransactionBiglimit(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#getSubAccount(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse getSubAccount(IVRRequest request) {

		String userid;
		String logonpasswd;
		String subAccountid;

		SubAccount subaccount;
		userid = request.getUserId();
		logonpasswd = request.getLoginpasswd();
		subAccountid = request.getSubaccountid();
		IVRResponse response = new IVRResponse();
		Map map = new HashMap();
		map.put("username",userid);
		map.put("password",logonpasswd);
		try {

			AccountSummary accountSummary = getCommonService().userLogon(
					Constant.CALL_CENTER, map);
			
			List list = accountSummary.getSubAccountList();
			
			response.setSubAccountArray(WebServiceUtil.convertList2SubAccounts(list));
			response.setArrayLength(new Integer(list.size()));
			for (int i = 0; i < list.size(); i++) {
				subaccount = (SubAccount) list.get(i);
				if ( null!= subAccountid && subAccountid.equals(subaccount.getId().toString())) {
					response.setMoney(subaccount.getAccountBalance().getBalance());
					//continue;
				}				
			}

		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#getSubAccount(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse getSubAccountBalance(IVRRequest request) {

		String userid;
		String logonpasswd;
		String subAccountid;

		SubAccount subaccount;
		userid = request.getUserId();
		logonpasswd = request.getLoginpasswd();
		subAccountid = request.getSubaccountid();
		IVRResponse response = new IVRResponse();
		//Map map = new HashMap();
		//map.put("username",userid);
		//map.put("password",logonpasswd);
		response.setResult(IVRConstant.COMMON_RESULT_TRUE);	
		try {
			subaccount = getCommonService().findSubAccountBalance(Long.parseLong(userid), subAccountid);
			if ( null!= subaccount) {
				response.setMoney(subaccount.getAccountBalance().getBalance());
				response.setResult(IVRConstant.COMMON_RESULT_TRUE);				
			}
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#getSupportBank(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse getSupportBank(IVRRequest request) {
		// TODO in future.
		IVRResponse response = new IVRResponse();
		try {
			this.loginUser(request);
			List banklist = this.getAccountTemplate().getExistDefaultBank();
			BankFormbean[] bankarray = WebServiceUtil.convertList2Banks(banklist);
			response.setBankArray(bankarray);
			response.setArrayLength(new Integer(response.getBankArray().length));
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#getAccount(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse getAccount(IVRRequest request) {

		String userid;
		String logonpasswd;
		userid = request.getUserId();
		//cn.agiledata.bank.hibernateBean.
		logonpasswd = request.getLoginpasswd();
		IVRResponse response = new IVRResponse();
		response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		Map map = new HashMap();
		map.put("username",userid);
		map.put("password",logonpasswd);
		try {
			this.getCommonService().userLogon(
					cn.agiledata.bank.common.util.Constant.CALL_CENTER, map);
			
			Account account=this.getAccountByid(Long.parseLong(userid));
			if(account==null){
				response.setResult(IVRConstant.COMMON_RESULT_FAIL);
			}else{
				response.setResult(IVRConstant.COMMON_RESULT_TRUE);
				response.setIntegral(account.getTotalScores());
			}
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#PrepRegister(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse PrepRegister(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		Map paramMap = new HashMap();
		paramMap.put("email",request.getEmail());
		paramMap.put("loginPassword",request.getLoginpasswd());
		paramMap.put("payPassword",request.getPaymentpasswd());
		paramMap.put("type",request.getAccounttype());
		try {
			this.getCommonService().userRegister(Constant.CALL_CENTER, null,paramMap);
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);
		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		} catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#getTransactionPoundage(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse getTransactionPoundage(IVRRequest request) {

		Map parmMap = null;
		Map vnvPameMap = null;

		IVRResponse response = new IVRResponse();

		/**
		 * bank card payment
		 * 
		 * String accountId = (String)map.get("accountId"); //wap:账户id;call
		 * center:账户accountId String subAccountId =
		 * (String)map.get("subAccountId"); //wap:子账户id String payPassword =
		 * (String)map.get("payPassword"); //wap:支付密码;call center:IVR支付密码 String
		 * bankName = (String)map.get("bankName"); //所属银行 String bankNo =
		 * (String)map.get("bankNo"); //卡号 String money =
		 * (String)map.get("money"); //金额 String name = (String)map.get("name");
		 * //姓名
		 */
		if (IVRConstant.PAYMENT_BANKCARD_TYPE.equals(request.getTranstype())) {
			parmMap = new HashMap();
			String accountid = request.getUserId();
			String subaccountid = null;
			String payPaymentpasswd = request.getPaymentpasswd();
			String bankName = request.getBanktype();
			String bankNo = request.getBankcard();
			String money = request.getMoney().toString();

			parmMap.put("accountId", accountid); // wap:账户id;call
			// center:账户accountId
			parmMap.put("subAccountId", null); // wap:子账户id
			parmMap.put("payPassword", payPaymentpasswd); // wap:支付密码;call
			// center:IVR支付密码
			parmMap.put("bankName", bankName); // 所属银行
			parmMap.put("bankNo", bankNo); // 卡号
			parmMap.put("money", money); // 金额
			parmMap.put("name", accountid); // 姓名

		} else if (IVRConstant.PAYMENT_VNV_TYPE.equals(request.getTranstype()))
		/**
		 * String receiveAccountName = (String)map.get("receiveAccountName");
		 * //wap:收款方email/手机;call center:收款方accountId String money =
		 * (String)map.get("money"); //金额 String payPassword =
		 * (String)map.get("payPassword"); //wap:支付密码；call center:IVR支付密码 String
		 * subAccountId = (String)map.get("subAccountId"); //wap:子账户id String
		 * accountId = (String)map.get("accountId"); //wap:账户id;call
		 * center:账户accountId
		 * 
		 */
		{

			vnvPameMap = new HashMap();
			String receiveaccountid = request.getCredited();
			String moneyvnv = request.getMoney().toString();
			String paymentPasswd = request.getPaymentpasswd();
			String accountidvnv = request.getUserId();

			vnvPameMap.put("receiveAccountName", receiveaccountid); // wap:收款方email/手机;call
			// center:收款方accountId
			vnvPameMap.put("money", moneyvnv); // 金额
			vnvPameMap.put("payPassword", paymentPasswd); // wap:支付密码；call
			// center:IVR支付密码
			// putmap.put("subAccountId"); //wap:子账户id
			vnvPameMap.put("accountId", accountidvnv); // wap:账户id;call
			// center:账户accountId

			// String
		}

		try {

			if (IVRConstant.PAYMENT_BANKCARD_TYPE
					.equals(request.getTranstype())) {
				response.setPoundage(getCommonService().transfer(
						Constant.CALL_CENTER, Constant.STEP_FIRST, parmMap)
						.getDebitFee());
			} else if (IVRConstant.PAYMENT_VNV_TYPE.equals(request
					.getTranstype())) {
				response.setPoundage(getCommonService().vnvpayment(
						Constant.CALL_CENTER,Constant.STEP_FIRST, vnvPameMap)
						.getDebitFee());

			}
			response.setResult(IVRConstant.COMMON_RESULT_TRUE);

		}catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		} catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}catch(Exception ex){
		     ex.printStackTrace();
		     logger.error(ex);
		     response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}

		return response;
	} // ////////////////////////////////////////////////////////////////////////

	/**
	 * Get the account by account id.   
	 * @param accountid
	 * @return
	 */
	private Account getAccountByid(long accountid){
		Long account = new Long(accountid);
		Account accountModel = null;
		try{
			accountModel = this.getCommonService().getAccountByAccountID(account.toString());
		} catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
		}
		return accountModel;
	}
	
	/**
	 *    
	 * @param transactionid
	 * @return
	 */
	private Double getTransactionPoundage(String transactionid) {

		Double paundage = new Double(0.0);
		try {
			paundage = getTansactionTemplement().getTransaction(transactionid)
					.getDebitFee();
		} catch (IPBException ipbex) {
			ipbex.printStackTrace();
			logger.error(ipbex);
		}
		return paundage;
	}

	/**
	 * 
	 * @param accountid
	 * @return
	 */
	private SubAccount getIVRSubAccount(long accountid) {
	
		SubAccount subaccount ;
		Account account = this.getAccountByid(accountid);
		subaccount = (SubAccount)account.getIvrSubAccounts().toArray()[0];
		return subaccount;
	}

	/**
	 * Get myspace service instance
	 * 
	 * @return
	 * @throws IPBException
	 */
	private IMySpaceService getMySpaceService() throws IPBException {
		return (IMySpaceService) IPBAppFactory.getObj(IMySpaceService.MGR_ID);
	}



	/**
	 * 
	 * @return
	 * @throws IPBException
	 */
	private TransactionTemplate getTansactionTemplement() throws IPBException {
		return (TransactionTemplate) IPBAppFactory
				.getObj("transactionTemplate");
	}

	/**
	 * 
	 * @return
	 * @throws IPBException
	 */
	private ICommonService getCommonService() throws IPBException {
		return (ICommonService) IPBAppFactory
				.getObj(ICommonService.SERVICE_NAME);
	}

	/**
	 * Get the unspay service
	 * 
	 * @return
	 * @throws IPBException
	 */
	private WebPayService getWebPayService() throws IPBException {
		return (WebPayService) IPBAppFactory.getObj(ForeDict.SERVICE_WEB_PAY);
	}

	/**
	 * Get the unspay service
	 * 
	 * @return
	 * @throws IPBException
	 */
	private AccountTemplateInter getAccountTemplate() throws IPBException {

		return (AccountTemplateInter) IPBAppFactory.getObj("accountTemplate");
	}

	/**
	 * Get the unspay service
	 * 
	 * @return
	 * @throws IPBException
	 */
	private ActionTypeTemplate getActionTemplate() throws IPBException {

		return (ActionTypeTemplate) IPBAppFactory.getObj("actionTypeTemplate");
	}
	
	/**
	 *    
	 * @param userid
	 * @param logonpasswd
	 * @return
	 */
	private AccountSummary getAccountSummary( String userid,String logonpasswd){
		Map map = new HashMap();
		map.put("username",userid);
		map.put("password",logonpasswd);		
		//Account account;
		try {
			return  getCommonService().userLogon(
					cn.agiledata.bank.common.util.Constant.CALL_CENTER, map);
			
		} catch (IPBException ipex) {
			ipex.printStackTrace();
			logger.error(ipex);
			
		}

		return null;
	}
	
	/**
	 * 银行卡列表
	 * @param accountID
	 * @see cn.agiledata.bank.webService.faced.IIVRWebServiceFaced#accountList(cn.agiledata.bank.webService.formbean.IVRRequest)
	 */
	public IVRResponse accountList(IVRRequest request) {
		IVRResponse response = new IVRResponse();
		String userid = request.getUserId();
		String logonpasswd = request.getLoginpasswd();
		String pageSize = request.getPageSize();
		String pageNum = request.getPageNum();
		AccountSummary as = getAccountSummary(userid,logonpasswd);
		try {
			if(as != null) {
				String accountId = String.valueOf(as.getAccountId());
				List list = getCommonService().accountList(accountId);
				if(list == null) {
					throw BusinessException.NO_RESULT;
				}
				if(list.size()==0) {
					throw BusinessException.NO_RESULT;
				}
				List list1 = getSubList(list,pageSize,pageNum);
				AccountListFormbean[] beans = WebServiceUtil.convertList2AccountList(list1);
				response.setHasMore(hasMoreList(list,pageSize,pageNum));
				response.setAccountListArray(beans);
				response.setResult(IVRConstant.COMMON_RESULT_TRUE);
				response.setArrayLength(new Integer(beans.length));
			}
		} catch(BusinessException be){
		    logger.error(be);
		    response.setResult(be.getResultCode());
		}catch (IPBException ipbex) {
			ipbex.printStackTrace();
			logger.error(ipbex);
			response.setResult(IVRConstant.COMMON_RESULT_FAIL);
		}
		return response;
	}
	/**
	 * 
	 * @param list
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	public List getSubList(List list,String p1,String p2) {
		if(list != null) {
			int pageSize = Integer.parseInt(p1);
			int pageNum = Integer.parseInt(p2);
			if(list.size() <= pageNum*pageSize+pageSize) {
				if(pageNum*pageSize <= list.size()) {
					return list.subList(pageNum*pageSize, list.size());
				} else {
					return new ArrayList();
				}
			} else {
				return list.subList(pageNum*pageSize, pageNum*pageSize+pageSize);
			}
			
		} else {
			return list;
		}
	}
	
	public String hasMoreList(List list,String p1,String p2) {
		if(list != null) {
			int pageSize = Integer.parseInt(p1);
			int pageNum = Integer.parseInt(p2);
			if(pageNum*pageSize+pageSize < list.size()) {
				return "true";
			} else {
				return "false";
			}
		} else {
			return "false";
		} 
	}
	
}